package com.user.read.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.user.read.dao.UserMapper;
import com.user.read.dto.UserDTO;
import com.user.read.service.UserService;
import com.user.read.util.PageRequest;
import com.user.read.util.PageResult;
import com.user.read.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserServiceImpl
 * @Desecription TODO 1.2新增
 * @Author gjy
 * @Date 2021/3/22 9:07
 * @Version 1.2
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserDTO> getAll() {
        return userMapper.getAll();
    }

    @Override
    public UserDTO getOne(int id) {
        return userMapper.getOne(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    /**
     * 调用分页插件完成分页
     *
     * @param pageRequest
     * @return
     */
    private PageInfo<UserDTO> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        Map map = new HashMap();
        map.put("sex", pageRequest.getSex());
        map.put("address", pageRequest.getAddress());
        map.put("studentNumber", pageRequest.getStudentNumber());
        map.put("type",pageRequest.getType());
        List<UserDTO> userDTOList = userMapper.getAllByPage(map);
        return new PageInfo<UserDTO>(userDTOList);
    }
}
